import request from '@/utils/request';

/**
 * 分页查询产品
 * @param params 查询条件
 */
export async function pageProducts(params) {
  const res = await request.get('/business/product/page', {
    params
  });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询产品列表
 * @param params 查询条件
 */
export async function listProducts(params) {
  const res = await request.get('/business/product', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询产品
 * @param id 产品id
 */
export async function getProduct(id) {
  const res = await request.get('/business/product/' + id);
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加产品
 * @param data 产品信息
 */
export async function addProduct(data) {
  const res = await request.post('/business/product', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改产品
 * @param data 产品信息
 */
export async function updateProduct(data) {
  const res = await request.put('/business/product', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除产品
 * @param id 产品id
 */
export async function removeProduct(id) {
  const res = await request.delete('/business/product/' + id);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除产品
 * @param data 产品id集合
 */
export async function removeProducts(data) {
  const res = await request.delete('/business/product/batch', {
    data
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改产品状态
 * @param ProductId 产品id
 * @param status 状态
 */
export async function updateProductStatus(id, statusFlag) {
  const res = await request.get('/business/product/status', {
    params: {
      id,
    }
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
